package finedev.android.venue;

import java.io.IOException;
import java.util.HashMap;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LoginActivity extends NetworkActivity implements OnClickListener {

	private final String TAG = "login";
	private Activity mActivity = null;
	
	public static HashMap<String, String> mLoginResult = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        mActivity = this;
        
        Button btn_login = (Button) findViewById(R.id.login_button_login);
        
        btn_login.setOnClickListener( this );
        
        
    }
    
    private void LoginSuccess()
    {
    	Log.i(TAG, "LoginSuccess()" );
//    	Intent intent = new Intent( mActivity, MainTabActivity.class );
    	startActivity( new Intent( getApplicationContext(), MainTabActivity.class ) );
    }
    
    private void LoginFail()
    {
    	Log.i(TAG, "LoginFail()" );
    }

	public void ReceiveSuccess(String str) {
		try {
			mLoginResult = new ObjectMapper().readValue( str, HashMap.class );
			if ( String.valueOf( mLoginResult.get("result") ).equals("true") ) {
				LoginSuccess();
			} else {
				LoginFail();
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	public void ReceiveFail(String str) {
		Log.i(TAG, "ReceiveFail()" );
	}

	public void onClick(View v) {
		if ( v.getId() == R.id.login_button_login ) {			
			
			ProgressDialog progressDialog = new ProgressDialog( mActivity );
			progressDialog.setMessage("Logging in...");
			progressDialog.setCancelable(false);
			
			NetworkManager loginManager = new NetworkManager( mActivity, progressDialog, "login" );
			
//			loginManager.execute( "http://www.vennue.net/new/m_login.php", 
//			((EditText) findViewById(R.id.login_edit_email)).getText().toString(),
//			((EditText) findViewById(R.id.login_edit_password)).getText().toString() );
			
			loginManager.execute( "http://www.vennue.net/new/m_login.php", 
					String.valueOf("arsdale"),
					String.valueOf("parkjuram"),
					String.valueOf("123412") );
			
		}
	}
}
